package com.sunzhe.cms.web;

import com.ibeetl.admin.core.service.CorePlatformService;
import com.ibeetl.admin.core.web.JsonResult;
import com.ibeetl.cms.entity.CmsArticle;
import com.ibeetl.cms.service.CmsArticleService;
import com.ibeetl.cms.service.CmsCatalogService;
import com.ibeetl.cms.web.query.ArticleQuery;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 首页 接口
 */
@Controller
public class CmsController {

    private final Log log = LogFactory.getLog(this.getClass());
    private static final String MODEL = "";
    @Autowired
	CmsCatalogService catalogService;
	@Autowired
	Environment env;
	@Autowired
	CorePlatformService platformService;
	@Autowired
	private StringRedisTemplate redisClient ;
	@Autowired
	CmsArticleService articleService;
	@Autowired
	HttpServletRequest request;

    @RequestMapping(MODEL + "")
	public ModelAndView index(HttpServletRequest request) {
    	String page = "/index.html";
		ModelAndView view = new ModelAndView(platformService.getTpl() + page);
		return view;
	}

	@RequestMapping("/setget.html")
	public @ResponseBody String env(String para) {
		redisClient.opsForValue().set("testenv", para) ;
		String str = redisClient.opsForValue().get("testenv");
		return str;
	}
	@RequestMapping("/getSessionId")
	public @ResponseBody String gettSeesionId(HttpServletRequest request){
		HttpSession session = request.getSession();
		String id = session.getId();
		return id;
	}
    
    @RequestMapping(MODEL + "/{page}.do")
  	public ModelAndView page(HttpServletRequest request, @PathVariable("page") String page) {
    	ModelAndView view = new ModelAndView(platformService.getTpl() + "/" + page + ".html");
  		return view;
  	}

    @RequestMapping(MODEL + "/{dir}/{page}.do")
  	public ModelAndView page1(HttpServletRequest request, @PathVariable("dir") String dir, @PathVariable("page") String page) {
    	ModelAndView view = new ModelAndView(platformService.getTpl() + "/" + dir + "/" + page + ".html");
  		return view;
  	}
	@RequestMapping(MODEL + "/{dir1}/{dir2}/{page}.do")
	public ModelAndView page2(HttpServletRequest request, @PathVariable("dir1") String dir1, @PathVariable("dir2") String dir2, @PathVariable("page") String page) {
		ModelAndView view = new ModelAndView(platformService.getTpl() + "/" + dir1 + "/" + dir2 + "/" + page + ".html");
		return view;
	}

	@RequestMapping(MODEL + "/article/list.json")
	@ResponseBody
	public JsonResult<PageQuery> list(ArticleQuery condition) {
    	String pageSize = request.getParameter("pageSize");
    	String pageNo = request.getParameter("page");
    	if (StringUtils.isBlank(pageSize)) pageSize = "10";
    	if (StringUtils.isBlank(pageNo)) pageNo = "1";

		PageQuery<CmsArticle> page = articleService.queryByArticleQuery(condition, NumberUtils.toLong(pageSize),NumberUtils.toLong(pageNo));
		return JsonResult.success(page);
	}
}
